<template>
    <div>最新成交
        <Table :columns="columns1" :data="data1"  :row-class-name="rowClassName"></Table>
    </div>
</template>

<script>
import { latestTransaction } from '@/api/transaction'

export default {
    name: 'LatestDeal',
    props: {
        stockId: {
            type: Number,
            default: 1,
        },
    },
    data() {
        return {
            columns1: [
                {
                    title: '成交价格',
                    key: 'price',
                },
                {
                    title: '成交数量',
                    key: 'dealed',
                },
                {
                    title: '成交时间',
                    key: 'ctime',
                    render: (h, params) => {
                        const row = params.row
                        const color = parseFloat(row.ctime) > 0 ? 'danger' : 'success'
                        const text = row.ctime.split('T')[1].split('.')[0]
                        return h('el-tag', {
                            props: {
                                type: color,
                            },
                        }, text)
                    },
                },
            ],
            data1: [
                {
                    name: 'John Brown',
                    age: 18,
                    address: 'New York No. 1 Lake Park',
                    date: '2016-10-03',
                },
                {
                    name: 'Jim Green',
                    age: 24,
                    address: 'London No. 1 Lake Park',
                    date: '2016-10-01',
                },
            ],
            timer: '',
        }
    },
    methods: {
        rowClassName(row, index) {
            if (row.type == 0) {
                return 'demo-table-error-row'
            }
        },
        update() {
            latestTransaction({ stock: this.stockId }).then(res => {
                this.data1 = res.results
                console.log(res.results)
            })
        },
    },
    mounted() {
        this.update()
        this.timer = setInterval(this.update, 1000)
    },
    beforeDestroy() {
        clearInterval(this.timer)
    },
}
</script>

<style>
    .demo-table-error-row{
        background-color: #ff6600;
        color:rgb(246, 70, 93);
    }

</style>
